package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.combat.QuestionSoul;
import com.itheima.domain.db.Question;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.QuestionSoulMapper;
import com.itheima.service.db.QuestionSoulService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

//问题
@Service
public class QuestionSoulServiceImpl implements QuestionSoulService {
    @Autowired
    private QuestionSoulMapper questionSoulMapper;

    //查询所有的问题
    @Override
    public List<QuestionSoul> findQuestionSoulById(Integer questionnaireId) {
        QueryWrapper<QuestionSoul> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        List<QuestionSoul> questionSoulList = questionSoulMapper.selectList(queryWrapper);
        return questionSoulList;
    }

    //根据试题的id查询试题
    @Override
    public QuestionSoul findQuestionById(String questionId) {
        QuestionSoul questionSoul = questionSoulMapper.selectById(questionId);
        return questionSoul;
    }

    //后台添加灵魂测试的问题
    @Override
    public Integer addQuestion(QuestionSoul questionSoul) {
        questionSoulMapper.addQuestion(questionSoul);
        return questionSoul.getId();
    }

    //删除题目
    @Override
    public void dedeleteQuestion(int parseInt) {
        QueryWrapper<QuestionSoul> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", parseInt);
        questionSoulMapper.delete(queryWrapper);
    }

    //查询所有的题目
    @Override
    public PageBeanVo findQuestion(Integer pageNum, Integer pageSize, String questionnaireId) {
        IPage<QuestionSoul> page = new Page(pageNum, pageSize);
        QueryWrapper<QuestionSoul> queryWrapper = new QueryWrapper<>();
        if ("0".equals(questionnaireId)) {
            queryWrapper = null;
        } else {
            queryWrapper.eq("questionnaire_id", questionnaireId);
        }
        IPage<QuestionSoul> questionSoulIPage = questionSoulMapper.selectPage(page, queryWrapper);

        return new PageBeanVo(pageNum, pageSize, questionSoulIPage.getTotal(), questionSoulIPage.getRecords());
    }

    //修改题目
    @Override
    public void updateQuestionSoul(QuestionSoul questionSoul) {
        questionSoulMapper.updateById(questionSoul);
    }


}
